﻿using EnglishAuction.BUS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EnglishAuction
{
    public partial class Auction : System.Web.UI.Page
    {
        private void Top15UserWin()
        {
            fvNguoiThangGanDay.DataSource = null;
            fvNguoiThangGanDay.DataBind();
        }

        private void ProductAuction()
        {
            if (Request.QueryString["Id"] != null)
            {
                int id = int.Parse(Request.QueryString["Id"]);
                if (id > 0)
                {                    
                    DetailProduct product = SanPhamBUS.GetDetailsProduct(id);
                    lbGiaBanCoTheDau.Text = String.Format("{0:###,###}",product.GiaCoTheDau);
                    lbGiaCaoNhatHienTai.Text = String.Format("{0:###,###}",product.GiaCaoNhatHienTai);
                    lbGiaKhoiDiem.Text = String.Format("{0:###,###}",product.GiaKhoiDiem);
                    lbNguoiRaGiaCaoNhat.Text = product.NguoiCoGiaCaoNhat.ToString();
                    lbThoiGianRaGia.Text = String.Format("{0:hh:mm tt dd/MM/yyyy}", product.ThoiGian);
                    hlHinhAnh.ImageUrl = "~/images/" + product.HinhAnh.ToString();
                    txtGiaDau.Text = product.GiaCoTheDau.ToString();
                                        
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["CurrentUser"] != null)
                {
                    Top15UserWin();
                    ProductAuction();
                }
                else
                {
                    Session["UrlExisting"] = "Auction.aspx?Id=" + Request.QueryString["Id"];
                    Session["Error"] = "Vui Lòng Đăng Nhập Trước Khi Đấu Giá";
                    Response.Redirect("Login.aspx");
                }
            }
        }              

        protected void btnXacNhanGiaDau_Click(object sender, EventArgs e)
        {
            double giaDau = double.Parse(txtGiaDau.Text);
            int iDSanPham = int.Parse(Request.QueryString["Id"]);

            if (DauGiaBUS.CheckBoi50(giaDau))
            {
                if (DauGiaBUS.CheckPriceInput(giaDau, double.Parse(lbGiaBanCoTheDau.Text)))
                {
                    NguoiDung nguoiDung = (NguoiDung)Session["CurrentUser"];
                    DauGia dauGia = new DauGia();
                    dauGia.SanPhamId = iDSanPham;
                    dauGia.NguoiDungId = nguoiDung.Id;
                    dauGia.GiaHienTai = giaDau;
                    dauGia.ThoiGian = DateTime.Now;
                    DauGiaBUS.Add(dauGia);

                    Response.Redirect("Default.aspx");
                }
                else
                {
                    lbThongBao.Text = "Giá Đấu Phải >= Giá Có Thể Đấu";                    
                }
            }
            else
            {
                lbThongBao.Text = "Vui Lòng Nhập Giá Là Bội 50.000";                
            }
        }

            
    }
}